/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.view;

import Utils.MyOwnFocusTraversalPolicy;
import Utils.TextFieldFilter;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JPopupMenu.Separator;
import javax.swing.border.Border;
import javax.swing.event.ChangeListener;

/**
 *
 * @author borysfan
 */
public class PaxSubPanel extends ContentPanel {

    private MyLabel paxLab;
    private MyLabel maleLab;
    private MyLabel femaleLab;
    private MyLabel childLab;
    private MyLabel infantLab;
    private MyLabel ttlPaxLab;
    private MyLabel classLab;
    private MyLabel cLab;
    private MyLabel yLab;
    private MyLabel maxCLab;
    private MyLabel maxYLab;
    private MyLabel ttlClassLab;
    private MyLabel sectionsLab;
    private MyLabel OALab;//tu jest duże 'o' a nie zero
    private MyLabel maxOALab;
    private MyLabel OBLab;//jw
    private MyLabel maxOBLab;
    private MyLabel OCLab;//jw
    private MyLabel maxOCLab;
    private MyLabel ttlSectionsLab;
    private MyLabel paxWeightLab;
    private MyTextField maleText;
    private MyTextField femaleText;
    private MyTextField childText;
    private MyTextField infantText;
    private MyTextField ttlPax1Text;
    private MyTextField ttlPax2Text;
    private MyTextField ttlPax3Text;
    private MyTextField businessText;
    private MyTextField economyText;
    private MyTextField OAText;//tu jest dużo 'o' a nie zero
    private MyTextField OBText;//jw
    private MyTextField OCText;//jw
    private MyComboBox paxWeightCombo;
    private MySeparator sep1;
    private MySeparator sep2;
    private MySeparator sep3;
    private MyLabel validation1;
    private MyLabel validation2;
    private MyLabel validation3;
    private MyOwnFocusTraversalPolicy travelsalPolicy;

    public PaxSubPanel() {
        super();
        initPanel();
    }

    private void initPanel() {
        int LOCAL_TEXT_WIDTH = 3;
        int _l = LOCAL_TEXT_WIDTH;

        paxLab = new MyLabel("PAX", MyLabel.BOLD);
        maleLab = new MyLabel("M", MyLabel.BOLD);
        femaleLab = new MyLabel("F", MyLabel.BOLD);
        childLab = new MyLabel("CH", MyLabel.BOLD);
        infantLab = new MyLabel("INF", MyLabel.BOLD);
        ttlPaxLab = new MyLabel("TTL", MyLabel.BOLD);
        ttlPaxLab.setPreferredSize(new Dimension(ttlPaxLab.getPreferredSize().width + 5, ttlPaxLab.getPreferredSize().height));

        classLab = new MyLabel("CLASS", MyLabel.BOLD);
        cLab = new MyLabel("C", MyLabel.BOLD);
        yLab = new MyLabel("Y", MyLabel.BOLD);
        yLab.setPreferredSize(new Dimension(yLab.getPreferredSize().width + 5, yLab.getPreferredSize().height));
        maxCLab = new MyLabel("MAX 100", MyLabel.BOLD);
        maxYLab = new MyLabel("MAX 100", MyLabel.BOLD);
        ttlClassLab = new MyLabel("TTL", MyLabel.BOLD);
        ttlClassLab.setPreferredSize(new Dimension(ttlClassLab.getPreferredSize().width + 5, ttlPaxLab.getPreferredSize().height));

        sectionsLab = new MyLabel("SECTIONS", MyLabel.BOLD);
        OALab = new MyLabel("0A", MyLabel.BOLD);
        OALab.setPreferredSize(new Dimension(OALab.getPreferredSize().width + 5, OALab.getPreferredSize().height));
        OBLab = new MyLabel("0B", MyLabel.BOLD);
        OCLab = new MyLabel("0C", MyLabel.BOLD);
        maxOALab = new MyLabel("MAX 100", MyLabel.BOLD);
        maxOBLab = new MyLabel("MAX 100", MyLabel.BOLD);
        maxOCLab = new MyLabel("MAX 100", MyLabel.BOLD);
        ttlSectionsLab = new MyLabel("TTL", MyLabel.BOLD);
        ttlSectionsLab.setPreferredSize(new Dimension(ttlSectionsLab.getPreferredSize().width + 5, ttlPaxLab.getPreferredSize().height));


        paxWeightLab = new MyLabel("PAX WEIGHT", MyLabel.BOLD);
        maleText = new MyTextField(_l);
        maleText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 3, false));
        femaleText = new MyTextField(_l);
        femaleText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 3, false));
        childText = new MyTextField(_l);
        childText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 3, false));
        infantText = new MyTextField(_l);
        infantText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 3, false));
        ttlPax1Text = new MyTextField(_l);
        ttlPax2Text = new MyTextField(_l);
        ttlPax3Text = new MyTextField(_l);
        ttlPax1Text.disable();
        ttlPax2Text.disable();
        ttlPax3Text.disable();
        businessText = new MyTextField(_l);
        businessText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 3, false));
        economyText = new MyTextField(_l);
        economyText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 3, false));
        OAText = new MyTextField(_l);
        OAText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 2, false));
        OBText = new MyTextField(_l);
        OBText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 2, false));
        OCText = new MyTextField(_l);
        OCText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 2, false));
        paxWeightCombo = new MyComboBox();
        paxWeightCombo.setPreferredSize(new Dimension(3 * paxWeightLab.getPreferredSize().width, paxWeightLab.getPreferredSize().height));
        paxWeightCombo.setFocusable(true);
        sep1 = new MySeparator();
        sep2 = new MySeparator();
        sep3 = new MySeparator();

        validation1 = new MyLabel("                                   ");
        // validation1.setPreferredSize(new Dimension(150,validation1.getHeight()));
        validation1.setForeground(Color.red);
        validation1.setVisible(false);
        validation2 = new MyLabel("                                   ");
        validation2.setVisible(false);
        validation2.setForeground(Color.red);
        validation3 = new MyLabel("                                   ");
        validation3.setVisible(false);
        validation3.setForeground(Color.red);

        setLayout(null);

        add(sep1);
        add(sep2);
        add(sep3);
        add(paxLab);
        add(maleLab);
        add(femaleLab);
        add(childLab);
        add(infantLab);
        add(ttlPaxLab);

        add(classLab);
        add(cLab);
        add(yLab);
        add(maxCLab);
        add(maxYLab);
        add(ttlClassLab);

        add(sectionsLab);
        add(OALab);
        add(OBLab);
        add(OCLab);
        add(maxOALab);
        add(maxOBLab);
        add(maxOCLab);
        add(paxWeightLab);
        add(ttlSectionsLab);

        add(maleText);
        add(femaleText);
        add(childText);
        add(infantText);
        add(ttlPax1Text);

        add(businessText);
        add(economyText);
        add(ttlPax2Text);

        add(OAText);
        add(OBText);
        add(OCText);
        add(ttlPax3Text);

        add(paxWeightCombo);
        //add(validation1);
        //add(validation2);
        //add(validation3);

        int firstCol = 150;
        int secondCol = 230;
        int thirdCol = 320;
        int forthCol = 410;
        int fifthCol = 530;


        int firstRow = 15;
        int secondRow = 70;
        int thirdRow = 135;

        paxLab.setPositionByRight(new Point(100, firstRow));
        maleLab.setPositionByRight(new Point(firstCol, firstRow));
        femaleLab.setPositionByRight(new Point(secondCol, firstRow));
        childLab.setPositionByRight(new Point(thirdCol, firstRow));
        infantLab.setPositionByRight(new Point(forthCol, firstRow));
        ttlPaxLab.setPositionByRight(new Point(fifthCol, firstRow));

        int _t = OAText.getPreferredSize().width;
        maleText.setPosition(new Point(firstCol, firstRow));
        femaleText.setPosition(new Point(secondCol, firstRow));
        childText.setPosition(new Point(thirdCol, firstRow));
        infantText.setPosition(new Point(forthCol, firstRow));
        ttlPax1Text.setPosition(new Point(fifthCol, firstRow));

        sep1.setPosition(new Point(0, firstRow + 45));
        sep1.setSize(578, 1);

        classLab.setPositionByRight(new Point(100, secondRow));
        cLab.setPositionByRight(new Point(firstCol, secondRow));
        yLab.setPositionByRight(new Point(secondCol, secondRow));
        maxCLab.setPositionByRight(new Point(firstCol + 50, secondRow + 27));
        maxYLab.setPositionByRight(new Point(secondCol + 50, secondRow + 27));
        ttlClassLab.setPositionByRight(new Point(fifthCol, secondRow));

        businessText.setPosition(new Point(firstCol, secondRow));
        economyText.setPosition(new Point(secondCol, secondRow));
        ttlPax2Text.setPosition(new Point(fifthCol, secondRow));
        //validation2.setPosition(new Point(fifthCol-_t+ttlPax2Text.getWidth()+5, secondRow+30));

        sep2.setPosition(new Point(0, secondRow + 50));
        sep2.setSize(578, 1);


        sectionsLab.setPositionByRight(new Point(100, thirdRow));
        OALab.setPositionByRight(new Point(firstCol, thirdRow));
        OBLab.setPositionByRight(new Point(secondCol, thirdRow));
        OCLab.setPositionByRight(new Point(thirdCol, thirdRow));
        maxOALab.setPositionByRight(new Point(firstCol + 50, thirdRow + 27));
        maxOBLab.setPositionByRight(new Point(secondCol + 50, thirdRow + 27));
        maxOCLab.setPositionByRight(new Point(thirdCol + 50, thirdRow + 27));
        OAText.setPosition(new Point(firstCol, thirdRow));
        OBText.setPosition(new Point(secondCol, thirdRow));
        OCText.setPosition(new Point(thirdCol, thirdRow));
        ttlSectionsLab.setPositionByRight(new Point(fifthCol, thirdRow));
        ttlPax3Text.setPosition(new Point(fifthCol, thirdRow));


        // validation3.setPosition(new Point(fifthCol-_t+ttlPax3Text.getWidth()+5, thirdRow+25));


        sep3.setPosition(new Point(0, thirdRow + 55));
        sep3.setSize(578, 1);

        paxWeightLab.setPosition(new Point(10, 213));
        paxWeightCombo.setPosition(new Point(150, 213));
        setOrderList();
    }

    void reset() {
        maleText.setText("0");
        femaleText.setText("0");
        childText.setText("0");
        infantText.setText("0");
        ttlPax1Text.setText("0");


        businessText.setText("0");
        economyText.setText("0");
        ttlPax2Text.setText("0");

        OAText.setText("0");
        OBText.setText("0");
        OCText.setText("0");
        ttlPax3Text.setText("0");

        paxWeightCombo.removeAllItems();
    }

    public void setOrderList() {
        Vector<Component> focusOrder = new Vector<Component>();

        focusOrder.add(maleText);
        focusOrder.add(femaleText);
        focusOrder.add(childText);
        focusOrder.add(infantText);

        focusOrder.add(businessText);
        focusOrder.add(economyText);

        focusOrder.add(OAText);
        focusOrder.add(OBText);
        focusOrder.add(OCText);
        focusOrder.add(paxWeightCombo);
        travelsalPolicy = new MyOwnFocusTraversalPolicy(focusOrder);
        this.setFocusTraversalPolicy(travelsalPolicy);
        this.setFocusTraversalPolicyProvider(true);
        this.setFocusCycleRoot(true);
    }

    public void addPaxWeightComboBoxItem(Object item) {
        paxWeightCombo.addItem(item);
    }

    public Object getPaxWeightComboBoxSelected() {
        return paxWeightCombo.getSelectedItem();
    }

    public String getMaleText() {
        return maleText.getText();
    }

    public String getFemaleText() {
        return femaleText.getText();
    }

    public String getChildText() {
        return childText.getText();
    }

    public String getInfantText() {
        return infantText.getText();
    }

    public String getCText() {
        return businessText.getText();
    }

    public String getYText() {
        return economyText.getText();
    }

    public String get0AText() {
        return OAText.getText();
    }

    public String get0BText() {
        return OBText.getText();
    }

    public String get0CText() {
        return OCText.getText();
    }

    public void setMaxCLab(String value) {
        maxCLab.setText(value);
    }

    public void setMaxYLab(String value) {
        maxYLab.setText(value);
    }

    public void setMaxOALab(String value) {
        maxOALab.setText(value);
    }

    public void setMaxOBLab(String value) {
        maxOBLab.setText(value);
    }

    public void setMaxOCLab(String value) {
        maxOCLab.setText(value);
    }

    public void setTTLPax1Text(String value) {
        ttlPax1Text.setText(value);
    }

    public void setTTLPax2Text(String value) {
        ttlPax2Text.setText(value);
    }

    public void setTTLPax3Text(String value) {
        ttlPax3Text.setText(value);
    }

    void setMaleFocusListener(FocusListener listener) {
        maleText.addFocusListener(listener);
    }

    void setFemaleFocusListener(FocusListener listener) {
        femaleText.addFocusListener(listener);
    }

    void setChildFocusListener(FocusListener listener) {
        childText.addFocusListener(listener);
    }

    void setInfantFocusListener(FocusListener listener) {
        infantText.addFocusListener(listener);
    }

    void setCFocusListener(FocusListener listener) {
        businessText.addFocusListener(listener);
    }

    void setYFocusListener(FocusListener listener) {
        economyText.addFocusListener(listener);
    }

    void set0AFocusListener(FocusListener listener) {
        OAText.addFocusListener(listener);
    }

    void set0BFocusListener(FocusListener listener) {
        OBText.addFocusListener(listener);
    }

    void set0CFocusListener(FocusListener listener) {
        OCText.addFocusListener(listener);
    }

    public String get_vpaxm() {
        return maleText.getText();
    }

    public String get_vpaxf() {
        return femaleText.getText();
    }

    public String get_vpaxch() {
        return childText.getText();
    }

    public String get_vpaxi() {
        return infantText.getText();
    }

    public String get_vpaxc() {
        return businessText.getText();
    }

    public String get_vpaxy() {
        return economyText.getText();
    }

    public String get_vpax0a() {
        return OAText.getText();
    }

    public String get_vpax0b() {
        return OBText.getText();
    }

    public String get_vpax0c() {
        return OCText.getText();
    }

    public String get_vttlpax1() {
        return ttlPax1Text.getText();
    }

    public String get_vttlpax2() {
        return ttlPax2Text.getText();
    }

    public String get_vttlpax3() {
        return ttlPax3Text.getText();
    }

    public String get_vpaxweights() {
        return (String) paxWeightCombo.getSelectedItem();
    }

    public void setPaxWeightsCombo(ActionListener listener) {
        this.paxWeightCombo.addActionListener(listener);
    }

    public void setValidator_1_Text(String s) {
        validation1.setText(s);
    }

    public void setValidator_2_Text(String s) {
        validation2.setText(s);
    }

    public void setValidator_3_Text(String s) {
        validation3.setText(s);
    }

    public void setVisibleValidator_1(boolean value) {
        validation1.setVisible(value);
    }

    public void setVisibleValidator_2(boolean value) {
        validation2.setVisible(value);
    }

    public void setVisibleValidator_3(boolean value) {
        validation3.setVisible(value);
    }

    public void setMaleBorder(Color c) {
        if (c.equals(Color.red)) {
            maleText.setName("invalid");
        } else {
            maleText.setName(null);
        }
        maleText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    public void setFemaleBorder(Color c) {
        if (c.equals(Color.red)) {
            femaleText.setName("invalid");
        } else {
            femaleText.setName(null);
        }
        femaleText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    public void setChildBorder(Color c) {
        if (c.equals(Color.red)) {
            childText.setName("invalid");
        } else {
            childText.setName(null);
        }
        childText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    public void setInfantBorder(Color c) {
        if (c.equals(Color.red)) {
            infantText.setName("invalid");
        } else {
            infantText.setName(null);
        }
        infantText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    public void setCBorder(Color c) {
        if (c.equals(Color.red)) {
            businessText.setName("invalid");
        } else {
            businessText.setName(null);
        }
        businessText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    public void setYBorder(Color c) {
        if (c.equals(Color.red)) {
            economyText.setName("invalid");
        } else {
            economyText.setName(null);
        }
        economyText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    public void setOABorder(Color c) {
        if (c.equals(Color.red)) {
            OAText.setName("invalid");
        } else {
            OAText.setName(null);
        }
        OAText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    public void setOBBorder(Color c) {
        if (c.equals(Color.red)) {
            OBText.setName("invalid");
        } else {
            OBText.setName(null);
        }
        OBText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    public void setOCBorder(Color c) {
        if (c.equals(Color.red)) {
            OCText.setName("invalid");
        } else {
            OCText.setName(null);
        }
        OCText.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    void setPaxTTL_1_Border(Color c) {
        if (c.equals(Color.red)) {
            ttlPax1Text.setName("invalid");
        } else {
            ttlPax1Text.setName(null);
        }
        ttlPax1Text.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    void setPaxTTL_2_Border(Color c) {
        if (c.equals(Color.red)) {
            ttlPax2Text.setName("invalid");
        } else {
            ttlPax2Text.setName(null);
        }
        ttlPax2Text.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    void setPaxTTL_3_Border(Color c) {
        if (c.equals(Color.red)) {
            ttlPax3Text.setName("invalid");
        } else {
            ttlPax3Text.setName(null);
        }
        ttlPax3Text.setBorder(BorderFactory.createLineBorder(c, 1));
    }

    public void setFirstActiveField() {
        maleText.requestFocusInWindow();
    }

    @Override
    public boolean isValid() {
        try {
            Component[] components = this.getComponents();
            for (int i = 0; i < components.length; i++) {
                //System.out.print( components[i].getClass().toString()+"\t"+components[i].getName() +"\n");
                if (components[i].getClass().toString().equals("class samoloty.view.MyTextField")) {
                    if (components[i].getName() != null) {
                        return false;
                    }
                }
            }
            return true;
        } catch (NullPointerException e) {
            return false;
        }
    }
}
